//
//  ChangeView.m
//  PetTown
//
//  Created by lanou3g on 15/5/29.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "ChangeView.h"

@implementation ChangeView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/


-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        //设置屏幕右下角切换按钮
        UIButton * changeButton = [UIButton buttonWithType:UIButtonTypeSystem];
        changeButton.frame = CGRectMake(0, 0, 50, 50);
        [changeButton setBackgroundImage:[UIImage imageNamed:@"qiehuan.png"] forState:UIControlStateNormal];
        [changeButton addTarget:self action:@selector(clickChangeBI:) forControlEvents:UIControlEventTouchDown];
        [self addSubview:changeButton];
        
    }
    return self;
}
//点击右下角按钮  弹出切换页面的四个button
- (void)clickChangeBI:(UIButton *)button{
    self.frame = self.window.frame;
    self.popupView = [[UIView alloc] init];
    self.popupView.frame = self.frame;
    UIImageView * backImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"qiehuanyemian.png"]];
    backImg.contentMode = UIViewContentModeScaleAspectFill;
    backImg.frame = self.popupView.frame;
    [self.popupView addSubview:backImg];
    //设置返回button
    UIButton * removeButton = [UIButton buttonWithType:UIButtonTypeSystem];
    removeButton.frame = CGRectMake(self.popupView.frame.size.width/2-50, self.popupView.frame.size.height-150, 100, 100);
    [removeButton setBackgroundImage:[UIImage imageNamed:@"fanhui.png"] forState:UIControlStateNormal];
    [removeButton addTarget:self action:@selector(removeView:) forControlEvents:UIControlEventTouchDown];
    //设置首页button
    self.homePageButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _homePageButton.frame = CGRectMake(self.popupView.frame.size.width/2-30, self.popupView.frame.size.height-40, 60, 60);
    [_homePageButton setBackgroundImage:[UIImage imageNamed:@"shouye.png"] forState:UIControlStateNormal];
    [self.homePageButton addTarget:self action:@selector(changeToHomePage:) forControlEvents:UIControlEventTouchDown];
    //设置发现button
    self.disCoverPageButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
    self.disCoverPageButton.frame = CGRectMake(self.popupView.frame.size.width/2-30, self.popupView.frame.size.height-40, 60, 60);
    [_disCoverPageButton setBackgroundImage:[UIImage imageNamed:@"faxian.png"] forState:UIControlStateNormal];
    [self.disCoverPageButton addTarget:self action:@selector(changeToDisCoverPage:) forControlEvents:UIControlEventTouchDown];
    //设置设置标签button
    self.tagPageButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
    self.tagPageButton.frame = CGRectMake(self.popupView.frame.size.width/2-30, self.popupView.frame.size.height-40, 60, 60);
    [_tagPageButton setBackgroundImage:[UIImage imageNamed:@"biaoqian.png"] forState:UIControlStateNormal];
    [self.tagPageButton addTarget:self action:@selector(changeToTagPage:) forControlEvents:UIControlEventTouchDown];
    //设置电台button
    self.radioPageButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
    self.radioPageButton.frame = CGRectMake(self.popupView.frame.size.width/2-30, self.popupView.frame.size.height-40, 60, 60);
    [_radioPageButton setBackgroundImage:[UIImage imageNamed:@"pianke.png"] forState:UIControlStateNormal];
    [self.radioPageButton addTarget:self action:@selector(changeToRadioPage:) forControlEvents:UIControlEventTouchDown];
    
    //将button添加到视图上
    [self.popupView addSubview:removeButton];
    [self.popupView addSubview:self.tagPageButton];
    [self.popupView addSubview:self.homePageButton];
    [self.popupView addSubview:self.disCoverPageButton];
    [self.popupView addSubview:self.radioPageButton];
    [self addSubview:_popupView];

    [self setSubViewAnimation];
}

//设置动画
- (void)setSubViewAnimation{
    //执行动画
    //view动画
    CATransition * animation = [CATransition animation];
    animation.duration = 1;
    animation.type = kCATransitionFade;
    [self.layer addAnimation:animation forKey:@"过渡动画"];
    //button动画
    [UIView animateWithDuration:1 animations:^{
        
        self.homePageButton.transform = CGAffineTransformMakeTranslation(- 100, -90);
        self.disCoverPageButton.transform = CGAffineTransformMakeTranslation(- 50, -200);
        self.tagPageButton.transform = CGAffineTransformMakeTranslation( 50, -200);
        self.radioPageButton.transform = CGAffineTransformMakeTranslation( 100, -90);
    }];
}

- (void)changeToHomePage:(UIButton *)button{
    if ([self.delegate respondsToSelector:@selector(ChangeView:didClickChangeWithButtonTag:)]) {
        [self.delegate ChangeView:self didClickChangeWithButtonTag:1];
        [self removePopView];
        [self setRemoveAnimation];
    }
}
- (void)changeToDisCoverPage:(UIButton *)button{
    if ([self.delegate respondsToSelector:@selector(ChangeView:didClickChangeWithButtonTag:)]) {
        [self.delegate ChangeView:self didClickChangeWithButtonTag:2];
        [self removePopView];
        [self setRemoveAnimation];
    }
}
- (void)changeToTagPage:(UIButton *)button{
    if ([self.delegate respondsToSelector:@selector(ChangeView:didClickChangeWithButtonTag:)]) {
        [self.delegate ChangeView:self didClickChangeWithButtonTag:3];
        [self removePopView];
        [self setRemoveAnimation];
    }
}
- (void)changeToRadioPage:(UIButton *)button{
    if ([self.delegate respondsToSelector:@selector(ChangeView:didClickChangeWithButtonTag:)]) {
        [self.delegate ChangeView:self didClickChangeWithButtonTag:4];
        [self removePopView];
        [self setRemoveAnimation];
    }
}
//设置返回button
- (void)removeView:(UIButton *)button{
    [self removePopView];
    [self setRemoveAnimation];
}
//设置移除View
- (void)removePopView{
    [self.popupView removeFromSuperview];
    self.frame = CGRectMake(self.window.frame.size.width-70, self.window.frame.size.height-70, 50, 50);
}
//设置动画
- (void)setRemoveAnimation{
    //执行动画
    //view动画
    CATransition * animation = [CATransition animation];
    animation.duration = 1;
    animation.type = kCATransitionFade;
    [self.layer addAnimation:animation forKey:@"过渡动画"];
}

@end
